import {Component, OnInit, ViewChild} from '@angular/core';
import {CONTRACT} from "../../service/contract.service";
import {CONSTANT} from "../../service/constant.service";
import {ExchangeModalViewComponent} from "../exchange-modal-view/exchange-modal-view.component";
import {CheckoutModalViewComponent} from "../checkout-modal-view/checkout-modal-view.component";
import {LocalStorage} from "../../app.local-storage";
import {ROUTE} from "../../service/route.service";
import {CheckStandModel} from "../../model/checkstand.model";

@Component({
  selector: 'app-my-renting-view',
  templateUrl: './my-renting-view.component.html',
  styleUrls: ['./my-renting-view.component.scss']
})
export class MyRentingViewComponent implements OnInit {
  @ViewChild(ExchangeModalViewComponent) public exchangeModal:any;
  @ViewChild(CheckoutModalViewComponent) public checkoutModal:any;


  constructor(private contractApi:CONTRACT,
              private constant:CONSTANT,
              private ls:LocalStorage,
              private route:ROUTE) {
  }

  public apartmentName:string;
  public contractId:number;
  public billList:any = [];
  public contractList:any = [];
  public currentContract:any = {id: undefined, remainingDays: '- -'};

  loadContractBill(item:any) {
    let reqData:any = {
      contractId: item.id,
      type:1
    }
    this.contractApi.getBill(reqData, (res:any)=> {
      let list:any = [];
      for (let item of res.content) {
        let data:any = {
          id: item.id,
          houseNo: item.houseNo,
          money: item.money,
          status: item.status,
          statusName: item.statusName,
          type: item.type,
          typeName: item.typeName,
          uuid: item.uuid,
          startTime: item.startTime ? item.startTime.split(' ', 1)[0] : null
        };
        list.push(data);
      }
      this.billList = list;
      console.log(this.billList);
    })
  }

  loadContractList() {
    this.contractApi.getContractList((res:any)=> {
      let list:any = [];
      for (let item of res.content) {
        let data:any = {
          id: item.id,
          apartmentName: item.apartmentName,
          uuid: item.uuid,
          remainingDays: item.remainingDays,
          houseNo: item.houseNo,
          deposit: item.deposit,
          rental: item.rental,
          status: item.status,
          statusName: item.statusName,
          leaseStart: item.leaseStart.split(' ', 1)[0],
          leaseEnd: item.leaseEnd.split(' ', 1)[0]
        };
        if (this.currentContract.id == undefined && data.status == 8) {
          this.currentContract = data;
        }
        list.push(data);
      }
      this.contractList = list;
      //进入组件时，若存在已入住状态的合同，则优先获取已入住的，若无已入住，则获取第一个合同
      if (this.contractList.length > 0) {
        this.currentContract = this.contractList[0];
        this.contractApi.getContractMsg(this.currentContract.id, v => {
          this.currentContract.contractPdf = v.contractPdf;
        })
        this.loadContractBill(this.contractList[0]);
      }
    })
  }

  fontStyle(item:any) {
    // 合同状态，1-未签约，2-已签约，4-已退租，8-已续约，16-已作废
    // 交租记录状态: 1-待付款，2-已付款
    let fontColor = item.status == 1 ? 'skyblue' : (item.status == 2 ? '#90c31f' : (item.status == 4 ? 'orange' : (item.status == 8 ? 'hotpink' : 'red')));
    return {
      "color": fontColor
    }
  }

  getStatus(item:any):any {
    return item.status == 1 ? {
      name: "交房租",
      style: {color: 'orange', textDecoration: 'underline', cursor: 'pointer'},
      canClick: true
    } : {'name': '已交租', style: {color: '#7f7f7f'}};
  }

  gotoContract(pdf:any) {
    window.open(pdf);
  }

  payRent(item:any) {
    this.ls.remove('id');
    if (item.status == 1) {
      let checkstand = new CheckStandModel(item.uuid, item.typeName, item.money, item.id, 'personal/myrent')
      this.ls.setObject('checkstand', checkstand);
      this.route.goConfirmPaymentView(item.uuid);
    }
  }

  mouseOver(item:any) {
    if (item) {
      item.hover = true;
    }
  }

  mouseLeave(item:any) {
    if (item) {
      item.hover = false;
    }
  }

  pickContract(item:any) {
    this.currentContract = item;
    this.contractApi.getContractMsg(this.currentContract.id, v => {
      this.currentContract.contractPdf = v.contractPdf;
    })
    this.loadContractBill(item);
    // console.log('currentContract',this.currentContract);
  }

  showExchangeModal() {
    this.apartmentName = this.currentContract.apartmentName;
    this.contractId = this.currentContract.id
    this.exchangeModal.showModal();
  }

  showCheckoutModal() {
    this.contractId = this.currentContract.id;
    this.checkoutModal.isModalShown = true;
  }

  closeExchangeModal(result:any) {
    this.exchangeModal.onHidden();
  }

  closeCheckoutModal(result:any) {
    this.checkoutModal.onHidden();
  }

  ngOnInit() {
    this.loadContractList();
    if (this.ls.getObject('contractReqData')) {
      this.ls.remove('contractReqData');
    } else if (this.ls.get('id')) {
      this.ls.remove('id');
    }
  }

}
